// pages/mine/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgList: [
      'https://changle-oss.oss-cn-hangzhou.aliyuncs.com/boothImg/876159276899368960.png',
      'https://img0.baidu.com/it/u=1650612162,1547749329&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=320',
      'https://img0.baidu.com/it/u=3830083417,1769929952&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=182'
    ],
    userInfo: {
      name: "游客",
      url: "/img/default-avatar.png",
      vipCode: 0,
      vipName: '普通会员',
      orderNum: 0,
      rollBagNum: 0,
      integralNum: 0,
      trackNum: 0,
    },
    maintenances: [{
      id: "",
      iconUrl: "",
      title: ""
    }],
    userId: "", //userId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 获取登录页功能列表
  getLoginPageList() {
    // var userId = wx.getStorageSync('userId');
    getApp().request('get', 1, '/home/function', {
        'userId': this.data.userId,
      }, true)
      .then(res => {
        console.log(res, 'ressssssssss')
        if (res && res.data && res.code && res.code === 200) {
          this.setData({
            maintenances: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      })
  },

  // 获取用户信息
  getUserInfo() {
    // let userId = wx.getStorageSync('userId');
    if (this.data.userId) {
      getApp().request('get', 1, '/user/info', {
          'userId': this.data.userId,
        }, true)
        .then(res => {
          if (res && res.data && res.code && res.code === 200) {
            this.setData({
              userInfo: res.data
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        })
    }
  },

  // 更新登录状态
  updateUserLoginState() {
    // let userId = wx.getStorageSync('userId');
    let list = ['退出', '登录']
    let newArr = this.data.maintenances.map(item => {
      if (list.includes(item.title)) {
        if (this.data.userId) {
          item.title = list[0];
        } else {
          item.title = list[1];
        }
      }
      return item;
    })
    if (newArr) {
      this.setData({
        maintenances: newArr
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userId: wx.getStorageSync('userId')
    })
    this.getLoginPageList();
    this.updateUserLoginState();
    this.getUserInfo();
  },
  // 执行方法的函数
  executeMethod(e) {
    let _this = this;
    _this[e.currentTarget.dataset.item.id](e.currentTarget.dataset.item.id);
  },
  navigateToFeedback(e) {
    // 导航到意见反馈页面
    getApp().navigateTo('/pages/mine/feedback/index?param=' + encodeURIComponent(e));
  },
  navigateToContact(e) {
    // 导航到联系客服页面
    getApp().navigateTo('/pages/mine/contact/index?param=' + encodeURIComponent(e));
  },
  navigateToAbout(e) {
    // 导航到关于我们页面
    getApp().navigateTo('/pages/mine/about/index?param=' + encodeURIComponent(e));
  },
  recharge(){
     getApp().navigateTo('/pages/mine/recharge/index');
  },
  loginOrOut() {
    var userId = wx.getStorageSync('userId');
    if (userId) {
      try {
        // 已经登陆，清楚登录信息
        this.setData({
          userInfo: {
            name: "游客",
            url: "/img/default-avatar.png",
            vipCode: 0,
            vipName: '普通会员',
            orderNum: 0,
            rollBagNum: 0,
            integralNum: 0,
            trackNum: 0,
          }
        });
        wx.removeStorageSync('userId');
        getApp().navigateTo('/pages/mine/login/index');
      } catch (e) {
        wx.showToast({
          title: '退出有误，请稍后再试！!',
          icon: 'none'
        });
      }
    } else {
      getApp().navigateTo('/pages/mine/login/index');
    }
  }
})